<template>
  <div class="auth-container" v-if="!isAuthenticated">
    <el-form :model="form" class="auth-form" @submit.prevent="login">
      <h3>欢迎回来</h3>
      <p class="subtitle">请登录以继续使用</p>
      <el-form-item label="用户名" label-width="80px">
        <el-input v-model="form.username" placeholder="请输入用户名" />
      </el-form-item>
      <el-form-item label="密码" label-width="80px">
        <el-input v-model="form.password" type="password" placeholder="请输入密码" show-password />
      </el-form-item>
      <el-button type="primary" native-type="submit" class="submit-button" :loading="loadingBtn">
        {{ loadingBtn ? '登录中...' : '登录' }}
      </el-button>
      <p class="register-link">
        还没有账号？
        <router-link to="/register">立即注册</router-link>
      </p>
    </el-form>
  </div>

  <div v-else class="redirect-message">
    <p>您已登录</p>
    <el-button type="danger" @click="logout">退出登录</el-button>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '../store/auth'
import { ElMessage } from 'element-plus'

const form = ref({
  username: '',
  password: '',
})
const authStore = useAuthStore()
const router = useRouter()

// 新增 loadingBtn 控制按钮加载状态
const loadingBtn = ref(false) // ✅ 添加这一行

// 检查是否已登录
const isAuthenticated = ref(false)

onMounted(() => {
  if (authStore.isAuthenticated) {
    //别压，一直压到首页
   //router.push({ name: 'Home' })
  } else {
    isAuthenticated.value = false
  }
})

const login = async () => {
  try {
    const username: string = form.value.username
    const password: string = form.value.password

    if (!username || !password) {
      ElMessage.error('请输入用户名和密码')
      return
    }

    loadingBtn.value = true // ✅ 开始加载

    await authStore.login(username, password)

    if (authStore.isAuthenticated) {
      ElMessage.success('登录成功')
      router.push({ name: 'Home' })
    } else {
      ElMessage.error('登录失败，请检查用户名和密码')
    }
  } catch (error) {
    console.error('登录错误:', error)
    ElMessage.error('登录失败，请稍后重试')
  } finally {
    loadingBtn.value = false // ✅ 结束加载
  }
}

// 新增 logout 方法
const logout = () => {
  authStore.logout()
  isAuthenticated.value = false
  ElMessage.success('已退出登录')
  router.push({ name: 'Login' }) // 可选：跳回登录页
}
</script>

<style scoped>
.auth-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 40px 20px;
  box-sizing: border-box;
}

/* 可选：添加背景粒子动效（需额外库） */
/* 如果你喜欢可以引入 particles.js 或 canvas-nest.js */

.auth-form {
  width: 100%;
  max-width: 500px; 
  padding: 40px; 
  background-color: rgba(255, 255, 255, 0.98);
  border-radius: 16px;
  box-shadow: 0 15px 40px rgba(0, 0, 0, 0.2); 
  animation: fadeInUp 0.6s ease-out forwards;
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.auth-form h3 {
  text-align: center;
  margin-bottom: 10px;
  font-size: 24px;
  color: #333;
}

.subtitle {
  text-align: center;
  font-size: 14px;
  color: #666;
  margin-bottom: 20px;
}

.submit-button {
  width: 100%;
  margin-top: 10px;
  padding: 12px;
  font-size: 16px;
  border-radius: 8px;
  transition: all 0.3s ease;
  background: linear-gradient(to right, #4f46e5, #6d28d9);
  border: none;
}

.submit-button:hover {
  background: linear-gradient(to right, #3b34d1, #5a24c2);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.register-link {
  text-align: center;
  margin-top: 15px;
  font-size: 14px;
  color: #666;
}

.register-link a {
  color: #409EFF;
  text-decoration: none;
  font-weight: bold;
  margin-left: 5px;
}

.register-link a:hover {
  text-decoration: underline;
}
</style>